﻿/**
  KCAKCAK   CAKCAKC   AK      CA
 AK        KC     CA  AK    AK
CA         KC     CA  AK  KC
CA         KCAKCAKCA  AKCA
CA         KC     CA  AK  KC
 AK        KC     CA  AK    AK
  KCAKCAK  KC     CA  AK      CA

_________________________________________________________________
 China ataca Kamchatka - copyright 2007(c) Ruben Sainz de la Maza
 
	@class: 	Login
	@vertion: 	0.1
	@date:		28/11/07
				
	@language: 	ActionScript 3.0
	@author:	Ruben Sainz de la Maza
			
**/


package com.conelmate.cak.screens
{
	import com.conelmate.util.Trace;
	import com.conelmate.nc.Connect;
	import com.conelmate.nc.AbstractProxyClient;
	
	import flash.display.MovieClip;	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.net.Responder;
	import fl.events.ComponentEvent;	
	import fl.controls.TextInput;
	import fl.controls.Button;	
	
	

	public class Login extends AbstractProxyClient
	{	
		static	public	const 	LOGGED			:String = "LOGGED";	
		static	public	const 	ERROR			:String = "ERROR";
		
		static internal var		_INSTANCE		:Login;			//instancia de esta clase (singleton)
				private	var 	_objConnect		:Connect;		//ref a la clase de coneccion al servidor FMS
						
				private var 	_MC;							//attach library screens/login/mcLogin
				private var 	_mcLogin;						//attach library screens/login/mcLogin
				private var 	_mcConnecting;					//attach library screens/mcConnecting
				private var 	_mcError;						//attach library screens/mcError
				private var 	_iArea			:TextInput;
				private var 	_bSend			:Button;
				private var		_strNick		:String;		//nick del usuario
		
		
//_________________________________________________________________________________ Constructor		
		public function Login(enforcer:SingletonEnforcer)
		{
			_isTrace = false;
			mTrace("#Login# constructor");
		}


//_________________________________________________________________________________ Public Methods

		/**
		 * singleton
		 * devolver una instancia de la clase
		 */	
		public static function getInstance():Login
		{
			if(Login._INSTANCE == null) Login._INSTANCE = new Login(new SingletonEnforcer());
			return Login._INSTANCE;
		}
		
		
		/**
		 * inicializar
		 */		
		public function init(pContainer:MovieClip):void
		{
			mTrace("#Login# init");
			
			initializeProxy("Login"); //se ejecuta en AbstractProxyClient
			
			setupConnection();

			_MC = pContainer;					//attach library screens/login/mcLogin
			_mcLogin	  = _MC.mcLogin;
			_mcConnecting = _MC.mcConnecting;	//attach library screens/mcConnecting
			_mcError 	  = _MC.mcError;		//attach library screens/mcError
			
			
			_mcConnecting.visible = false;
			_mcError.visible = false;
			
			_iArea = _mcLogin.iArea;
			_bSend = _mcLogin.bSend;
			_iArea.addEventListener(ComponentEvent.ENTER, onPressEnter);
			_bSend.addEventListener(MouseEvent.CLICK, onSendButton);
			
			_iArea.setFocus();
		}
		
		
		/**
		 * destruir objetos y variables
		 */	
		public function destroy():void
		{
			mTrace("#Login# destroy");
			finalizeProxy();
			Login._INSTANCE = null;
		}
		
		
//_________________________________________________________________________________ Private Methods

		private function setupConnection():void
		{
			if(_objConnect) _objConnect.destroy();
			_objConnect = Connect.getInstance();
			_objConnect.addEventListener(Connect.CONNECTED, onConnect);
			_objConnect.addEventListener(Connect.CLOSED, 	onClosed);
			_objConnect.addEventListener(Connect.FAILED, 	onFailed);
			_objConnect.addEventListener(Connect.REJECTED, 	onRejected);
		}
		
		
		/**
		 * al presionar la tecla ENTER
		 */		
        private function onPressEnter(e:Event):void 
		{
			mTrace("#Login# onPressEnter");
			connectToServer();
        }
		

		/**
		 * al hacer clic en login
		 */		
		private function onSendButton(e:MouseEvent):void 
		{
			mTrace("#Login# onSendButton");
			var clicked:String = e.target.name;
			connectToServer();
		}
		
		
		/**
		 * conectar al servidor
		 */		
		private function connectToServer():void
		{
			mTrace("#Login# connectToServer");
			
			_mcLogin.visible = false;
			_mcError.visible = false;
			_mcConnecting.visible = true;

			_strNick = _iArea.text;
			var o:Object = new Object;
			o.strNick = _strNick;
			
			_objConnect.arguments = o;
			_objConnect.init();
		}
		
		
		/**
		 * al conectar, iniciar el administrador de pantallas
		 */		
		private function onConnect(e:Event):void
		{
			mTrace("#Login# onConnect");
			_iArea.removeEventListener(ComponentEvent.ENTER, onPressEnter);
			_bSend.removeEventListener(MouseEvent.CLICK, onSendButton);
			_mcLogin.visible = false;
			_mcConnecting.visible = false;
			_mcError.visible = false;
			
			dispatchEvent(new Event(Login.LOGGED));			
		}
		

		/**
		 * al cerrarse la coneccion
		 */		
		private function onClosed(e:Event):void
		{
			mTrace("#Login# onClosed");
			dispatchEvent(new Event(Login.ERROR));
		}
		
		
		/**
		 * al fallar la coneccion
		 */		
		private function onFailed(e:Event):void
		{
			mTrace("#Login# onFailed " + e);
			dispatchEvent(new Event(Login.ERROR));
		}
		
		
		/**
		 * al rechazar la coneccion
		 */		
		private function onRejected(e:Event):void
		{
			mTrace("#Login# onRejected " + e);
			
			var error:String = e.target.error;

			_mcConnecting.visible = false;
			_mcError.visible = true;
			_mcError.texto.htmlText = "ERROR!<br>" + e.target.msg;
			_mcLogin.visible = true;

			dispatchEvent(new Event(Login.ERROR));			
			setupConnection();
/*
			if(error = "usuarioRepetido"){
				
			}
			
			if(error = "servidorFull"){
			}
*/			
			
//FALTA IMPLEMENTAR USUARIO REPETIDO
		//	trace("Error: "+e.target.error);
		//	trace("MSG: "+e.target.msg);
			
		}


		public function get nick():String { return _strNick; }
	}
}

internal class SingletonEnforcer{}